﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace DataAccess
{
    public class PhanCongGiangDay
    {
        // cac thuoc tinh:
        private string _maGV;
        public string MaGV
        {
            get { return _maGV; }
            set { _maGV = value; }
        }

        private string _temGV;
        public string TemGV
        {
            get { return _temGV; }
            set { _temGV = value; }
        }

        private string _maMH;
        public string MaMH
        {
            get { return _maMH; }
            set { _maMH = value; }
        }

        private string _tenMH;
        public string TenMH
        {
            get { return _tenMH; }
            set { _tenMH = value; }
        }

        private int _hocKy;
        public int HocKy
        {
            get { return _hocKy; }
            set { _hocKy = value; }
        }

        private string _namHoc;
        public string NamHoc
        {
            get { return _namHoc; }
            set { _namHoc = value; }
        }

        // cac phuong thuc tao lap:

        public PhanCongGiangDay() { }

        public PhanCongGiangDay(string gv, string mh, int hk,string nh)
        {
            MaGV = gv;
            MaMH = mh;
            HocKy = hk;
            NamHoc = nh;
        }

        public PhanCongGiangDay(PhanCongGiangDay pc)
        {
            MaGV = pc.MaGV;
            MaMH = pc.MaMH;
            HocKy = pc.HocKy;
            NamHoc = pc.NamHoc;
        }

        //cac phuong thuc khai thac:

        public static int Add(PhanCongGiangDay pc)
        {
            try
            {
                using (SqlConnection connec = MyConnection.MyConn())
                {
                    SqlCommand cmd = MyCommand.Command("PhanCongGiangDay_Add", connec);
                    cmd.Parameters.Add("@MaGV", SqlDbType.VarChar, 10).Value = pc.MaGV;
                    cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = pc.MaMH;
                    cmd.Parameters.Add("@HocKy", SqlDbType.Int).Value = pc.HocKy;
                    cmd.Parameters.Add("@NamHoc", SqlDbType.VarChar, 20).Value = pc.NamHoc;
                    connec.Open();
                    int after = cmd.ExecuteNonQuery();
                    return after;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return 0;
            }
            
        }

        public static int Delete(string magv, string mamh)
        {
            using (SqlConnection connec= MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("PhanCongGiangDay_Delete", connec);
                cmd.Parameters.Add("@MaGV", SqlDbType.VarChar, 10).Value = magv;
                cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = mamh;
                connec.Open();
                int num = cmd.ExecuteNonQuery();
                return num;
            }
        }
        public static int Delete(PhanCongGiangDay pc)
        {
            try
            {
                using (SqlConnection connec = MyConnection.MyConn())
                {
                    SqlCommand cmd = MyCommand.Command("PhanCongGiangDay_Delete", connec);
                    cmd.Parameters.Add("@MaGV", SqlDbType.VarChar, 10).Value = pc.MaGV;
                    cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = pc.MaMH;
                    cmd.Parameters.Add("@HK", SqlDbType.Int).Value = pc.HocKy;
                    cmd.Parameters.Add("@Nam", SqlDbType.VarChar, 20).Value = pc.NamHoc;
                    connec.Open();
                    int num = cmd.ExecuteNonQuery();
                    return num;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return 0;
            }
            
        }

        public static List<PhanCongGiangDay> All()
        {
            List<PhanCongGiangDay> list = new List<PhanCongGiangDay>();
            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("PhanCongGiangDay_All", connec);
                connec.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhanCongGiangDay pc = GetGV2(reader);
                        list.Add(pc);
                    }
                }
            }
            return list;
        }

        public static List<PhanCongGiangDay> PhancongGiangDay_LoadNamHoc(string Magv)
        {
            List<PhanCongGiangDay> list = new List<PhanCongGiangDay>();
            using (SqlConnection connec= MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("PhancongGiangDay_LoadNamHoc", connec);
                cmd.Parameters.Add("@MaGV", SqlDbType.VarChar, 10).Value = Magv;
                connec.Open();
                using (SqlDataReader reader= cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhanCongGiangDay pc = new PhanCongGiangDay();
                        pc.NamHoc = reader["NamHoc"].ToString();
                        list.Add(pc);
                    }
                }
            }
            return list;
        }
        public static List<PhanCongGiangDay> PhancongGiangDay_LoadNamHoc_HocKy(string namhoc)
        {
            List<PhanCongGiangDay> list = new List<PhanCongGiangDay>();
            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("PhancongGiangDay_LoadNamHoc_HocKy", connec);
                cmd.Parameters.Add("@NamHoc", SqlDbType.VarChar, 20).Value = namhoc;
                connec.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhanCongGiangDay pc = new PhanCongGiangDay();
                        pc.HocKy = Convert.ToInt32(reader["HocKy"]);
                        list.Add(pc);
                    }
                }
            }
            return list;
        }

        public static List<PhanCongGiangDay> FindByMaGV( string ma)
        {
            List<PhanCongGiangDay> list = new List<PhanCongGiangDay>();
            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("PhanCongGiangDay_FindByMaGV", connec);
                cmd.Parameters.Add("@MaGV", SqlDbType.VarChar, 10).Value = ma;
                connec.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhanCongGiangDay pc = GetGV2(reader);
                        list.Add(pc);
                    }
                }
            }
            return list;
        }
        public static List<PhanCongGiangDay> FindByTenGV(string ten)
        {
            List<PhanCongGiangDay> list = new List<PhanCongGiangDay>();
            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("PhanCongGiangDay_FindByTenGV", connec);
                cmd.Parameters.Add("@TenGV", SqlDbType.NVarChar, 50).Value = ten;
                connec.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhanCongGiangDay pc = GetGV2(reader);
                        list.Add(pc);
                    }
                }
            }
            return list;
        }

        public static List<PhanCongGiangDay> FindByMaMH(string ma)
        {
            List<PhanCongGiangDay> list = new List<PhanCongGiangDay>();
            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("PhanCongGiangDay_FindByMaMH", connec);
                cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = ma;
                connec.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhanCongGiangDay pc = GetGV2(reader);
                        list.Add(pc);
                    }
                }
            }
            return list;
        }

        public static List<PhanCongGiangDay> FindByTenMH(string tenmh)
        {
            List<PhanCongGiangDay> list = new List<PhanCongGiangDay>();
            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("PhanCongGiangDay_FindByTenMH", connec);
                cmd.Parameters.Add("@TenMH", SqlDbType.NVarChar, 200).Value = tenmh;
                connec.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhanCongGiangDay pc = GetGV2(reader);
                        list.Add(pc);
                    }
                }
            }
            return list;
        }

        public static List<PhanCongGiangDay> FindByHocKy(string hk)
        {
            List<PhanCongGiangDay> list = new List<PhanCongGiangDay>();
            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("PhanCongGiangDay_FindByHocKy", connec);
                cmd.Parameters.Add("@HocKy", SqlDbType.VarChar, 10).Value = hk;
                connec.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhanCongGiangDay pc = GetGV2(reader);
                        list.Add(pc);
                    }
                }
            }
            return list;
        }

        public static List<PhanCongGiangDay> FindByNamHoc(string nam)
        {
            List<PhanCongGiangDay> list = new List<PhanCongGiangDay>();
            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("PhanCongGiangDay_FindByNamHoc", connec);
                cmd.Parameters.Add("@Nam", SqlDbType.VarChar, 20).Value = nam;
                connec.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        PhanCongGiangDay pc = GetGV2(reader);
                        list.Add(pc);
                    }
                }
            }
            return list;
        }
        
        // cac ham bo tro:
        private static PhanCongGiangDay GetGV(SqlDataReader reader)
        {
            PhanCongGiangDay pc = new PhanCongGiangDay();
            pc.MaGV = reader["MaGV"].ToString();
            pc.MaMH = reader["MaMH"].ToString();
            pc.HocKy = Convert.ToInt32(reader["HocKy"]);
            pc.NamHoc = reader["NamHoc"].ToString();
            return pc;
        }

        private static PhanCongGiangDay GetGV2(SqlDataReader reader)
        {
            PhanCongGiangDay pc = new PhanCongGiangDay();
            pc.MaGV = reader["MaGV"].ToString();
            pc.TemGV = reader["TenGV"].ToString();
            pc.MaMH = reader["MaMH"].ToString();
            pc.TenMH = reader["TenMH"].ToString();
            pc.HocKy = Convert.ToInt32(reader["HocKy"]);
            pc.NamHoc = reader["NamHoc"].ToString();
            return pc;
        }
    }
}
